package com.pdsu.graduation_design.views;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.domain.User;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.MyDBOpenHelper;
import com.pdsu.graduation_design.utils.Result;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class UserLoginActivity extends AppCompatActivity {
    private MyDBOpenHelper myDBOpenHelper;
    private SQLiteDatabase db;

    private EditText username_ed,password_ed;
    private Button login_button;
    private TextView tv_register;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //隐藏任务栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        operation();
    }
    void init(){
        username_ed=findViewById(R.id.username_edit);
        password_ed=findViewById(R.id.password_edit);
        login_button=findViewById(R.id.login_btn);
        tv_register=findViewById(R.id.tv_register);
        context=getApplicationContext();
        myDBOpenHelper=new MyDBOpenHelper(context,"my.db",null,3);

        db=myDBOpenHelper.getWritableDatabase();
    }
    void operation(){

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),UserRegisterActivity.class);
                startActivity(intent);
            }
        });


        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String username=username_ed.getText().toString();
                String password=password_ed.getText().toString();
                if ((username.length()==0)||(password.length()==0)){
                    Toast.makeText(getApplicationContext(),"账号或密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else {
                    //post请求获取
                    RequestParams requestParams= new RequestParams("http://10.0.2.2:8088/user/login");
                    requestParams.addBodyParameter("Username",username);
                    requestParams.addBodyParameter("Password",password);
                    x.http().get(requestParams, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {

                            try {
                                Result r= JsonUtil.JsonToObj(result,Result.class);
                                if (r.getCode().equals("200")){
                                    System.out.println("2000000"+r.getData());
                                    //将用户数据存入本地
                                    User u=JsonUtil.JsonToObj(r.getData().toString(),User.class);
                                    System.out.println(u.toString()+"+++++++++++++");
                                    /**
                                     * 更新本地数据库token
                                     */

                                    SQLiteDatabase db =new MyDBOpenHelper(context,"my.db",null,3).getWritableDatabase();
                                    ContentValues contentValues=new ContentValues();
                                    contentValues.put("username",u.getUsername());
                                    contentValues.put("usertoken",u.getToken());
                                    contentValues.put("phone",u.getPhone());
                                    contentValues.put("uid",u.getUid());
                                    System.out.println(u.toString()+"------------");
                                    long num=  db.insert("user02",null,contentValues);
                                    System.out.println(num);
                                    MainActivity.user_admin=u;
                                    //启动activity
                                    Intent intent=new Intent(getApplicationContext(), MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }else {
                                    Toast.makeText(getApplicationContext(), "账号或密码错误", Toast.LENGTH_SHORT).show();

                                }
                            } catch (JsonProcessingException e) {
                                e.printStackTrace();
                            }

                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {

                            System.out.println("error"+ex);
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {
                            System.out.println("can");

                        }

                        @Override
                        public void onFinished() {
                            System.out.println("fin");

                        }
                    });

                }

            }
        });




    }
}
